package cn.stylefeng.guns.modular.fi.controller;

import cn.hutool.core.collection.ListUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.entity.BasicLine;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicLineParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicLineResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicLineService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.fi.entity.FiCarrierFreightInfo;
import cn.stylefeng.guns.modular.fi.model.params.FiCarrierFreightInfoParam;
import cn.stylefeng.guns.modular.fi.model.params.YunFeiParam;
import cn.stylefeng.guns.modular.fi.service.FiCarrierFreightInfoService;
import cn.stylefeng.guns.modular.fi.utils.YunFeiUtils;
import cn.stylefeng.guns.modular.transport.service.TransportDispatchService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 承运商运费对账单控制器
 *
 * @author zhangzh
 * @Date 2024-06-21 08:48:21
 */
@Controller
@RequestMapping("/fiCarrierFreightInfo")
public class FiCarrierFreightInfoController extends BaseController {

    private String PREFIX = "/fi/fiCarrierFreightInfo";

    @Autowired
    private FiCarrierFreightInfoService fiCarrierFreightInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private BasicLineService basicLineService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiCarrierFreightInfo.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //项目
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        //承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        //线路
        BasicLineParam lineParam = new BasicLineParam();
        lineParam.setDeliverId(deliverId);
        lineParam.setDeptId(deptId);
        if(user.getOperateId() != null){
            lineParam.setOperateId(user.getOperateId().intValue());
        }
        List<BasicLineResult> lineLists = basicLineService.findListBySpec(lineParam, user.getOperateId());
        model.addAttribute("lineLists", lineLists);

        return PREFIX + "/fiCarrierFreightInfo_add.html";
    }

    @RequestMapping("/addByCarrier")
    public String addByCarrier(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //项目
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        //承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        return PREFIX + "/fiCarrierFreightInfo_addByCarrier.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //项目
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        //承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        //线路
        BasicLineParam lineParam = new BasicLineParam();
        lineParam.setDeliverId(deliverId);
        lineParam.setDeptId(deptId);
        if(user.getOperateId() != null){
            lineParam.setOperateId(user.getOperateId().intValue());
        }
        List<BasicLineResult> lineLists = basicLineService.findListBySpec(lineParam, user.getOperateId());
        model.addAttribute("lineLists", lineLists);
        return PREFIX + "/fiCarrierFreightInfo_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FiCarrierFreightInfoParam fiCarrierFreightInfoParam) {
        User user = userService.getcurrentUser();
        //根据项目ID获取项目名称
        BasicOperate Operate = basicOperateService.getById(fiCarrierFreightInfoParam.getOperateId());
        fiCarrierFreightInfoParam.setOperateName(Operate.getOperateName());
        //根据承运商ID获取供应商名称
        BasicCarrier carrier = basicCarrierService.getById(fiCarrierFreightInfoParam.getCarrierId());
        fiCarrierFreightInfoParam.setCarrierName(carrier.getCarrierName());
        //根据线路ID获取线路名称
        BasicLine line = basicLineService.getById(fiCarrierFreightInfoParam.getLineId());
        fiCarrierFreightInfoParam.setLineName(line.getLineName());

        fiCarrierFreightInfoParam.setDeliverId(user.getDeliverId());
        fiCarrierFreightInfoParam.setDeptId(user.getDeptId());
        fiCarrierFreightInfoParam.setCreateUser(user.getUserId());
        fiCarrierFreightInfoParam.setStat("暂存");
        //总费用
        fiCarrierFreightInfoParam.setSumMoney(YunFeiUtils.getSumMoney(fiCarrierFreightInfoParam));

        this.fiCarrierFreightInfoService.add(fiCarrierFreightInfoParam);
        return ResponseData.success();
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("/addItemByCarrier")
    @ResponseBody
    public ResponseData addItemByCarrier(FiCarrierFreightInfoParam fiCarrierFreightInfoParam) {
        User user = userService.getcurrentUser();
        //根据项目ID获取项目名称
        BasicOperate Operate = basicOperateService.getById(fiCarrierFreightInfoParam.getOperateId());
        fiCarrierFreightInfoParam.setOperateName(Operate.getOperateName());
        //根据承运商ID获取供应商名称
        BasicCarrier carrier = basicCarrierService.getById(fiCarrierFreightInfoParam.getCarrierId());
        fiCarrierFreightInfoParam.setCarrierName(carrier.getCarrierName());
        fiCarrierFreightInfoParam.setDeliverId(user.getDeliverId());
        fiCarrierFreightInfoParam.setDeptId(user.getDeptId());
        fiCarrierFreightInfoParam.setCreateUser(user.getUserId());
        fiCarrierFreightInfoParam.setStat("暂存");

        this.fiCarrierFreightInfoService.addByCarrier(fiCarrierFreightInfoParam);
        return ResponseData.success();
    }


    /**
     * 修改状态
     * @param fiCarrierFreightInfoParam
     * @return
     */
    @RequestMapping("/updateStat")
    @ResponseBody
    public ResponseData updateStat(FiCarrierFreightInfoParam fiCarrierFreightInfoParam) {
        this.fiCarrierFreightInfoService.update(fiCarrierFreightInfoParam);
        return ResponseData.success();
    }

    @RequestMapping("/duizhang")
    @ResponseBody
    public ResponseData duizhang(FiCarrierFreightInfoParam fiCarrierFreightInfoParam) {
        FiCarrierFreightInfo obj = this.fiCarrierFreightInfoService.getById(fiCarrierFreightInfoParam.getId());
        if(!"暂存".equals(obj.getStat())){
            return ResponseData.error("非【暂存】状态，不能发起对账");
        }
        User user = userService.getByAccount(obj.getCarrierUserCode());
        if(user == null){
            return ResponseData.error("承运商账号不存在，请修改后重试");
        }
        this.fiCarrierFreightInfoService.update(fiCarrierFreightInfoParam);
        return ResponseData.success();
    }



    @RequestMapping("/duizhangBatch")
    @ResponseBody
    public ResponseData duizhangBatch(@RequestParam("ids") String ids) {
        List<String> list = ListUtil.of(ids.split(","));
        int i = this.fiCarrierFreightInfoService.idxStatIsOk(list);
        if(i > 0){
            return ResponseData.error("选择记录中含有非【暂存】状态，不能发起对账");
        }
        this.fiCarrierFreightInfoService.updateByids(list);
        return ResponseData.success();
    }


    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FiCarrierFreightInfoParam fiCarrierFreightInfoParam) {
        FiCarrierFreightInfo obj = this.fiCarrierFreightInfoService.getById(fiCarrierFreightInfoParam.getId());
        if("已确认".equals(obj.getStat())){
            return ResponseData.error("【已确认】状态，不能修改");
        }
        //根据项目ID获取项目名称
        BasicOperate Operate = basicOperateService.getById(fiCarrierFreightInfoParam.getOperateId());
        fiCarrierFreightInfoParam.setOperateName(Operate.getOperateName());
        //根据承运商ID获取供应商名称
        BasicCarrier carrier = basicCarrierService.getById(fiCarrierFreightInfoParam.getCarrierId());
        fiCarrierFreightInfoParam.setCarrierName(carrier.getCarrierName());
        //根据线路ID获取线路名称
        BasicLine line = basicLineService.getById(fiCarrierFreightInfoParam.getLineId());
        fiCarrierFreightInfoParam.setLineName(line.getLineName());
        //总费用
        fiCarrierFreightInfoParam.setSumMoney(YunFeiUtils.getSumMoney(fiCarrierFreightInfoParam));

        this.fiCarrierFreightInfoService.update(fiCarrierFreightInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FiCarrierFreightInfoParam fiCarrierFreightInfoParam) {
        //查询当前对账单的状态，只有暂存的可以删除
        FiCarrierFreightInfo obj = this.fiCarrierFreightInfoService.getById(fiCarrierFreightInfoParam.getId());
        if(!"暂存".equals(obj.getStat())){
            return ResponseData.error("非【暂存】状态，不能删除");
        }
        this.fiCarrierFreightInfoService.delete(fiCarrierFreightInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FiCarrierFreightInfoParam fiCarrierFreightInfoParam) {
        FiCarrierFreightInfo detail = this.fiCarrierFreightInfoService.getById(fiCarrierFreightInfoParam.getId());
        return ResponseData.success(detail);
    }


    /**
     * 获取费用
     * @param param
     * @return
     */
    @RequestMapping("/getYunFei")
    @ResponseBody
    public ResponseData getYunFei(YunFeiParam param) {
        return this.fiCarrierFreightInfoService.getYunFei(param);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-06-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FiCarrierFreightInfoParam fiCarrierFreightInfoParam,String condition ) {
        if(ToolUtil.isEmpty(fiCarrierFreightInfoParam.getStartTime())){
            fiCarrierFreightInfoParam.setStartTime(DateUtil.getDate());
        }
        if(ToolUtil.isEmpty(fiCarrierFreightInfoParam.getEndTime())){
            fiCarrierFreightInfoParam.setEndTime(DateUtil.getDate());
        }
        User user = userService.getcurrentUser();
        fiCarrierFreightInfoParam.setCreateUser(user.getUserId());
        return this.fiCarrierFreightInfoService.findPageBySpec(fiCarrierFreightInfoParam,condition);
    }

}


